.PHONY: all build run tool clean help push

BINARY="sqlite-logs-more"

run:
	@echo "开始运行go程序【直接运行】"
	@go run ./main.go -web=":8081" -grpc=":8082" -root-path="./root.db"

all: tool build

build:
	@echo "开始编译到linux-amd64系统下..."
	@CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags "-s -w -X main.REMOTE_ADDR=grpc://43.138.16.245:9092" -o ${BINARY} ./main.go
	@git checkout go.mod > /dev/null 2>&1
	@echo "编译完成"

tool:
	@echo "开始格式化程序代码..."
	@go fmt ./
	@go vet ./
	@go mod tidy
	@git checkout go.mod > /dev/null 2>&1
	@echo "格式化完成"

clean:
	@echo "开始清理编译文件..."
	@if [ -f ${BINARY} ] ; then rm ${BINARY} ; fi
	@echo "清理完成"

help:
	@echo "make - 格式化 Go 代码, 并编译生成二进制文件【此处为生成linux-amd64系统下的命令，且该命令会设置运行模式等方法】"
	@echo "make build - 编译 Go 代码, 生成二进制文件"
	@echo "make run - 直接运行 Go 代码"
	@echo "make clean - 移除二进制文件和 vim swap files"
	@echo "make tool - 运行 Go 工具 'fmt' and 'vet'"
	@echo "make push - 推送编译后的文件到正式服务器上，并重启该服务"

push:tool build
	@echo "开始推送程序到服务器上..."
	@scp ${BINARY} public_user@43.138.16.245:/var/www/sqlite_logs_more/sqlite_logs_more_new
	@echo "推送完成"
	@echo "开始重启服务器上的程序..."
	@ssh public_user@43.138.16.245 "cd /var/www/sqlite_logs_more;bash update.sh"
	@ssh public_user@43.138.16.245 "cd /var/www/sqlite_logs_more;bash check.sh"
	@echo "重启完成"
	@echo "开始重启服务器上的正式网站程序，避免登录异常..."
	@ssh public_user@43.138.16.245 "cd /var/www/golang_gin;bash check.sh"
	@echo "重启完成"
	@echo "开始清理编译文件..."
	@if [ -f ${BINARY} ] ; then rm ${BINARY} ; fi
	@echo "清理完成"